package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwdiT;
import cn.e4j.modular.crm.mapper.GwdiTMapper;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuan_rz
 * @since 2019-07-26
 */
@Service
public class GwdiTServiceImpl extends ServiceImpl<GwdiTMapper, GwdiT> {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Transactional(readOnly = true)
    public int getMaxGwal002(Dto inDto) {
        int count = this.baseMapper.getMaxGwdi(inDto);
        return count;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdiList(Page<Object> page, Dto inDto) {
        List<Map<String, Object>> list = this.baseMapper.getGwdiList(page, inDto);
        return list;
    }

    @Transactional(readOnly = true)
    public Dto getDetailData(String GWDI001) {
        Dto dto = this.baseMapper.getDetailData(GWDI001);
        return dto;
    }

    @Transactional
    public void updateStatus(Dto dto) {
        UpdateWrapper<GwdiT> gwdiTUpdateWrapper = new UpdateWrapper<>();
        gwdiTUpdateWrapper.eq("GWDI001", dto.getString("GWDI001"));
        GwdiT gwdiT = new GwdiT();
        gwdiT.setGWDI006("Y");
        gwdiT.setGWDI007("Y");
        gwdiT.update(gwdiTUpdateWrapper);
    }

    @Transactional
    public void update(Dto inDto) {
        ShiroUser user = ShiroKit.getUser();
        List<Map<String, Object>> list = sysRoleMapper.listRolesOfUser(user.getID());
        List<Dto> role = new ArrayList<>();
        for(Map<String, Object> item : list){
            Dto dto = Dtos.newDto();
            if(item.get("NAME").toString().equals("财务部")){
                dto.put("role", "渠道价格");
                role.add(dto);
                Dto pDto = Dtos.newDto();
                pDto.put("role", "组合促销活动");
                role.add(pDto);
                Dto pDto1 = Dtos.newDto();
                pDto1.put("role", "非组合促销活动");
                role.add(pDto1);
                Dto pDto2 = Dtos.newDto();
                pDto2.put("role", "合同协议");
                role.add(pDto2);
                Dto pDto3 = Dtos.newDto();
                pDto3.put("role", "订单变更");
                role.add(pDto3);
            } else if(item.get("NAME").toString().equals("技术部")){
                dto.put("role", "产品资料维护");
                role.add(dto);
            }else if(item.get("NAME").toString().equals("销售部助理")){
                dto.put("role", "注册信息维护");
                role.add(dto);
            } else if(item.get("NAME").toString().equals("销售总监")){
                dto.put("role", "客户资料维护");
                role.add(dto);
            }
        }
        inDto.put("role",role);
        this.baseMapper.updateGwdi(inDto);
    }

    @Transactional(readOnly = true)
    public List<Dto> getUserList(Dto inDto) {
        List<Dto> list = this.baseMapper.getUserList(inDto);
        return list;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwaa(Dto inDto) {
        List<Dto> list = this.baseMapper.getGwaa(inDto);
        return list;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwamList(Dto inDto) {
        List<Dto> list = this.baseMapper.getGwamList(inDto);
        return list;
    }

    @Transactional(readOnly = true)
    public List<Dto> getUser(Dto inDto) {
        List<Dto> list = this.baseMapper.getUser(inDto);
        return list;
    }
}
